﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;

namespace WXYW
{
    public partial class ReturnToFactoryConfirmReceive : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitialPage();
            }
        }

        private void InitialPage()
        {
            ReturnToFactoryService service = new ReturnToFactoryService();
            ReturnToFactoryForm form = service.GetReturnToFactoryFormProductDetail(long.Parse(Request.QueryString[Constants.QUERYSTR_RETURNTOFACTORY_ID]));

            ProviderService providerservice = new ProviderService();
            Provider provider = providerservice.GetProviderByCode(form.ProvideCode);
            if (provider != null)
            {
                providerNameLabel.Text = provider.ProviderName;
                connectPersonLabel.Text = provider.Contact1Name;
                if (!string.IsNullOrEmpty(provider.Contact2Name))
                {
                    connectPersonLabel.Text += "; " + provider.Contact2Name;
                }
                proviceCodeLabel.Text = provider.ProviderCode;
                addressLabel.Text = provider.Address;
                phoneNumberLabel.Text = provider.Contact1Phone;
                if (!string.IsNullOrEmpty(provider.Contact2Phone))
                {
                    phoneNumberLabel.Text += "; " + provider.Contact2Phone;
                }
                cellPhoneLabel.Text = provider.Contact1MobilePhone;
                if (!string.IsNullOrEmpty(provider.Contact2MobilePhone))
                {
                    cellPhoneLabel.Text += "; " + provider.Contact2MobilePhone;
                }

                workStatusLabel.Text = provider.Relation;
            }

            rmaCodeLabel.Text = form.ApplicationProduct.ApplicationForm.ApplicationCode;
            applicantEmailLabel.Text = form.ApplicationProduct.ApplicationForm.User.UserEmail;
            applicantLabel.Text = form.ApplicationProduct.ApplicationForm.User.FullName;
            applicantPhoneLabel.Text = form.ApplicationProduct.ApplicationForm.User.UserPhone;

            rtfRunningNumberLabel.Text = form.RunningNumber;
            facotryRMALabel.Text = form.Code;
            reasonOfCostLabel.Text = form.ApplicationProduct.ReasonOfCost;
            if (form.ApplicationProduct.IsDecideToRepair.HasValue)
            {
                isRepairLabel.Text = form.ApplicationProduct.IsDecideToRepair.Value ? "是" : "否";
            }
            rtfDateTimeLabel.Text = form.ApplyDate.Value.ToString("yyyy-MM-dd");
            productTypeLabel.Text = form.ApplicationProduct.ProductType.ProductTypeName;
            versionLabel.Text = form.ApplicationProduct.ProductVersionNumber;
            productSerialNumberLabel.Text = form.ApplicationProduct.ProductSerialNumber;
            problemDescriptionLabel.Text = form.ApplicationProduct.ProductProblemDescription;
            confirmQuatationLabel.Text = form.ConfirmQuatationDate.HasValue ? form.ConfirmQuatationDate.Value.ToString("yyyy-MM-dd") : "";
            quatationTextBox.Text = form.ProviderQuatationDate.HasValue ? form.ProviderQuatationDate.Value.ToString("yyyy-MM-dd") : "";

            double cost = 0;
            if (!form.ReturnToFactoryQuatation.IsLoaded)
            {
                form.ReturnToFactoryQuatation.Load();
            }
            int index = 0;
            foreach (ReturnToFactoryQuatation quatationItem in form.ReturnToFactoryQuatation)
            {
                index++;
                cost += quatationItem.Count * (double)quatationItem.UnitPrice;
                repairAccessoryTextBox.Text += quatationItem.AccessoryName;
                if (index < form.ReturnToFactoryQuatation.Count)
                {
                    repairAccessoryTextBox.Text += "; ";
                }
            }
            costLabel.Text = cost.ToString();
            receiveDateTxtBox.Text = form.ReceiveDate.HasValue ? form.ReceiveDate.Value.ToString("yyyy-MM-dd") : DateTime.Now.ToString("yyyy-MM-dd");
            if (!string.IsNullOrEmpty(form.ReceiveStatus))
            {
                if (form.ReceiveStatus.Equals("收货无误"))
                {
                    ReceiveStatusRadioButtonList.SelectedIndex = 0;
                }
                else if (form.ReceiveStatus.Equals("收货有误"))
                {
                    ReceiveStatusRadioButtonList.SelectedIndex = 1;
                }
            }
            checkDateTxtBox.Text = DateTime.Now.ToString("yyyy-MM-dd");
        }

        protected void saveLinkButton_Click(object sender, EventArgs e)
        {
            try
            {
                bool isValid = false;
                if (ReceiveStatusRadioButtonList.SelectedIndex == -1)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "tip", "alert('请填写收货确认信息!');", true);
                }
                else if (ReceiveStatusRadioButtonList.SelectedIndex == 0)
                {
                    if (CheckStatusRadioButtonList.SelectedIndex == -1)
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "tip", "alert('请填写货物验证信息!');", true);
                    }
                    else if (CheckStatusRadioButtonList.SelectedIndex == 1 && string.IsNullOrEmpty(checkRamrkTxtBox.Text))
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "tip", "alert('请填写验证异常信息!');", true);
                    }
                    else
                    {
                        isValid = true;
                    }
                }
                else if (string.IsNullOrEmpty(recieveRamrkTxtBox.Text))
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "tip", "alert('请填写收货异常信息!');", true);
                }
                else
                {
                    isValid = true;
                }
                if (isValid)
                {
                    if (ReceiveStatusRadioButtonList.SelectedIndex == 2)
                    {
                        //客户悔修
                        ReturnToFactoryService service = new ReturnToFactoryService();
                        ApplicationService appservice = new ApplicationService();
                        ReturnToFactoryForm form = service.GetReturnToFactoryFormByID(long.Parse(Request.QueryString[Constants.QUERYSTR_RETURNTOFACTORY_ID]));
                        form.Status = "客户悔修";
                        form.ReceiveRemark = recieveRamrkTxtBox.Text;
                        service.Update(form);

                        if (!form.ApplicationProductReference.IsLoaded)
                        {
                            form.ApplicationProductReference.Load();
                        }
                        form.ApplicationProduct.RepairCompleteDateTime = DateTime.Now;
                        form.ApplicationProduct.Status = "维修完毕待发货";
                        ApplicationProductService productservice = new ApplicationProductService();
                        productservice.UpdateProduct(form.ApplicationProduct);
                        //productservice.FinishMaintainProduct(form.ApplicationProduct);
                    }
                    else
                    {
                        // 客户没有悔修
                        ReturnToFactoryService service = new ReturnToFactoryService();
                        ApplicationService appservice = new ApplicationService();
                        ReturnToFactoryForm form = service.GetReturnToFactoryFormByID(long.Parse(Request.QueryString[Constants.QUERYSTR_RETURNTOFACTORY_ID]));
                        if (!form.ApplicationProductReference.IsLoaded)
                        {
                            form.ApplicationProductReference.Load();
                        }
                        if (!form.ApplicationProduct.ApplicationFormReference.IsLoaded)
                        {
                            form.ApplicationProduct.ApplicationFormReference.Load();
                        }
                        if (!form.ApplicationProduct.ApplicationForm.ApplicationProduct.IsLoaded)
                        {
                            form.ApplicationProduct.ApplicationForm.ApplicationProduct.Load();
                        }
                        if (!form.ApplicationProduct.UpgradeForm.IsLoaded)
                        {
                            form.ApplicationProduct.UpgradeForm.Load();
                        }
                        if (!form.ReturnToFactoryQuatation.IsLoaded)
                        {
                            form.ReturnToFactoryQuatation.Load();
                        }
                        if (!form.ApplicationProduct.QuatationForm.IsLoaded)
                        {
                            form.ApplicationProduct.QuatationForm.Load();
                        }
                        form.ReceiveDate = Convert.ToDateTime(receiveDateTxtBox.Text);
                        form.ReceiveStatus = ReceiveStatusRadioButtonList.SelectedValue;
                        form.ReceiveRemark = recieveRamrkTxtBox.Text;
                        form.CheckDate = Convert.ToDateTime(checkDateTxtBox.Text);
                        form.CheckStatus = CheckStatusRadioButtonList.SelectedValue;
                        form.CheckRemark = checkRamrkTxtBox.Text;

                        if (CheckStatusRadioButtonList.SelectedIndex == 0 && ReceiveStatusRadioButtonList.SelectedIndex == 0)
                        {
                            ApplicationProductService productService = new ApplicationProductService();
                            UpgradeService upgradeService = new UpgradeService();
                            // 没有报价
                            if (form.ApplicationProduct.QuatationForm.Count == 0)
                            {
                                // 升级产品
                                if (form.ApplicationProduct.UpgradeForm.Count > 0)
                                {
                                    foreach (UpgradeForm upgradeForm in form.ApplicationProduct.UpgradeForm)
                                    {
                                        if (upgradeForm.SendWay.Equals("平台"))
                                        {
                                            upgradeForm.Status = "升级-完毕待发货";
                                            upgradeService.Update(upgradeForm);
                                            form.Status = "返厂-维修完毕";
                                            form.ApplicationProduct.RepairCompleteDateTime = DateTime.Now;
                                        }
                                        else
                                        {
                                            upgradeForm.Status = "升级完毕";
                                            upgradeForm.CompleteTime = DateTime.Now;
                                            upgradeService.Update(upgradeForm);
                                            form.ApplicationProduct.Status = "维修完毕待发货";
                                            form.ApplicationProduct.RepairCompleteDateTime = DateTime.Now;
                                            form.Status = "返厂-维修完毕";
                                            //productService.FinishMaintainProduct(form.ApplicationProduct);
                                        }
                                    }
                                }
                                else
                                {
                                    form.ApplicationProduct.Status = "维修完毕待发货";
                                    form.ApplicationProduct.RepairCompleteDateTime = DateTime.Now;
                                    form.Status = "返厂-维修完毕";
                                    //productService.FinishMaintainProduct(form.ApplicationProduct);
                                }
                            }
                            // 客户报价了，工厂也报价了
                            else// if (form.ReturnToFactoryQuatation.Count > 0)
                            {
                                // 升级产品
                                if (form.ApplicationProduct.UpgradeForm.Count > 0)
                                {
                                    foreach (UpgradeForm upgradeForm in form.ApplicationProduct.UpgradeForm)
                                    {
                                        if (upgradeForm.SendWay.Equals("平台"))
                                        {
                                            upgradeForm.Status = "升级-完毕待发货";
                                            upgradeService.Update(upgradeForm);
                                            form.Status = "返厂-维修完毕";
                                            form.ApplicationProduct.RepairCompleteDateTime = DateTime.Now;
                                        }
                                        else
                                        {
                                            upgradeForm.Status = "升级完毕";
                                            upgradeForm.CompleteTime = DateTime.Now;
                                            upgradeService.Update(upgradeForm);

                                            form.ApplicationProduct.Status = "维修完毕待发货";
                                            form.ApplicationProduct.RepairCompleteDateTime = DateTime.Now;
                                            form.Status = "返厂-维修完毕";
                                            //productService.FinishMaintainProduct(form.ApplicationProduct);
                                        }
                                    }
                                }
                                else
                                {
                                    //if (!form.ApplicationProduct.Status.Equals("返厂-待客户报价确认"))
                                    //{
                                    //    form.ApplicationProduct.Status = "维修完毕待发货";
                                    //    form.ApplicationProduct.RepairCompleteDateTime = DateTime.Now;
                                    //}
                                    form.ApplicationProduct.RepairCompleteDateTime = DateTime.Now;
                                    form.Status = "返厂-维修完毕";
                                    //productService.FinishMaintainProduct(form.ApplicationProduct);
                                }
                            }
                            // 工厂没有报价
                            //else
                            //{ 

                            //}
                        }
                        // 二次返厂
                        else if (CheckStatusRadioButtonList.SelectedIndex == 1)
                        {
                            form.ApplicationProduct.Status = "返厂维修中";
                            form.IsSecondTimeRepair = true;
                            form.Status = "返厂-待发货";
                        }
                        // 如果收货有误 该如何处理？？
                        service.Update(form);
                    }

                    ClientScript.RegisterStartupScript(this.GetType(), "tip", "alert('信息保存成功!');location.href='ReturnToFactoryListConfirmReceive.aspx';", true);
                }
            }
            catch (Exception ex)
            {
                LogUtility.LogUtilityIntance.LogException(ex, "ReturnToFactoryConfirmReceive.aspx::saveLinkButton_Click");
                ClientScript.RegisterStartupScript(this.GetType(), "tip", string.Format("alert('信息保存发生错误，请截图保存该错误信息并联系管理员！错误原因：{0}');", ex.Message), true);
            }
        }

        private void BindData()
        {
            ReturnToFactoryService service = new ReturnToFactoryService();
            ReturnToFactoryForm form = service.GetReturnToFactoryFormProductDetail(long.Parse(Request.QueryString[Constants.QUERYSTR_RETURNTOFACTORY_ID]));
            ApplicationProduct product = form.ApplicationProduct;
            hfAppliyProductID.Value = form.ApplicationProduct.ApplicationProductID.ToString();
            serialLabel.Text = product.ProductSerialNumber;
            rmaMaintainLabel.Text = product.ApplicationForm.ApplicationCode;
            typeLabel.Text = product.ProductType.ProductTypeName;
            serialLabel.Text = product.ProductSerialNumber;
            versionNOLbl.Text = product.ProductVersionNumber;
            purchaseTimeLbl.Text = product.ProductPurchaseDateTime.Value.ToString("yyyy-MM-dd");
            blxxTextBox.Text = product.BLXX;
            if (!product.QuatationForm.IsLoaded)
            {
                product.QuatationForm.Load();
            }
            if (product.QuatationForm != null && product.QuatationForm.Count > 0)
            {
                double cost = 0;
                int index = 1;
                foreach (QuatationForm quatation in product.QuatationForm)
                {
                    cost += quatation.Cost;

                    switch (index)
                    {
                        case 1:
                            blljTextBox1.Text = quatation.AccessoryName;
                            break;
                        case 2:
                            blljTextBox2.Text = quatation.AccessoryName;
                            break;
                        case 3:
                            blljTextBox3.Text = quatation.AccessoryName;
                            break;
                        case 4:
                            blljTextBox4.Text = quatation.AccessoryName;
                            break;
                        case 5:
                            blljTextBox5.Text = quatation.AccessoryName;
                            break;
                    }
                    index++;
                }
                bjLabel.Text = string.Format("总金额:{0}", cost);

                WXLJGridView.DataSource = product.QuatationForm;
                WXLJGridView.DataBind();
            }
            else
            {
                bjLabel.Text = "未报价";
            }

            if (!string.IsNullOrEmpty(product.ChangeType))
            {
                if (product.ChangeType.Equals("更换"))
                {
                    ghRadioButton.Checked = true;
                }
                if (product.ChangeType.Equals("替换"))
                {
                    thRadioButton.Checked = true;
                }
            }

            if (!string.IsNullOrEmpty(product.ReplaceProductSerialNumber))
            {
                thSerialNumberTextBox.Text = product.ReplaceProductSerialNumber;
            }

            if (!string.IsNullOrEmpty(product.ReplaceProductType))
            {
                thProductTypeTextBox.Text = product.ReplaceProductType;
            }

            if (!string.IsNullOrEmpty(product.ReplaceProductVersion))
            {
                versionDDL.SelectedValue = product.ReplaceProductVersion;
            }

            if (!string.IsNullOrEmpty(product.RepairAccessory))
            {
                InitialRepaireAccessory(product.RepairAccessory);
            }

            if (!string.IsNullOrEmpty(product.AccessoryPlace))
            {
                InitialAccessoryPlace(product.AccessoryPlace);
            }

            if (!string.IsNullOrEmpty(product.ReasonToProblem))
            {
                InitializeBLYY(product.ReasonToProblem);
            }

            if (!product.ProblemAnalyzeReference.IsLoaded)
            {
                product.ProblemAnalyzeReference.Load();
            }

            if (product.ProblemAnalyze != null)
            {
                InitializeProblemAnalyze(product.ProblemAnalyze);
            }
            bzTextBox.Text = product.Remark;
        }

        private void InitializeProblemAnalyze(ProblemAnalyze problemAnalyze)
        {
            SetProblemAnalyzeA(problemAnalyze.A);
            SetProblemAnalyzeB(problemAnalyze.B);
            SetProblemAnalyzeC(problemAnalyze.C);
            SetProblemAnalyzeD(problemAnalyze.D);
            SetProblemAnalyzeE(problemAnalyze.E);
        }

        private void InitialAccessoryPlace(string accessoryPlace)
        {
            string[] accessorys = accessoryPlace.Split(';');
            for (int i = 0; i < accessorys.Count(); i++)
            {
                switch (i)
                {
                    case 0:
                        ljwzTextBox1.Text = accessorys[i];
                        break;
                    case 1:
                        ljwzTextBox2.Text = accessorys[i];
                        break;
                    case 2:
                        ljwzTextBox3.Text = accessorys[i];
                        break;
                    case 3:
                        ljwzTextBox4.Text = accessorys[i];
                        break;
                    case 4:
                        ljwzTextBox5.Text = accessorys[i];
                        break;
                }
            }
        }

        private void InitializeBLYY(string blyy)
        {
            string[] blyyArray = blyy.Split(';');
            for (int i = 0; i < blyyArray.Count(); i++)
            {
                switch (i)
                {
                    case 0:
                        blyyRadioButtonList1.SelectedValue = blyyArray[i];
                        break;
                    case 1:
                        blyyRadioButtonList2.SelectedValue = blyyArray[i];
                        break;
                    case 2:
                        blyyRadioButtonList3.SelectedValue = blyyArray[i];
                        break;
                    case 3:
                        blyyRadioButtonList4.SelectedValue = blyyArray[i];
                        break;
                    case 4:
                        blyyRadioButtonList4.SelectedValue = blyyArray[i];
                        break;
                }
            }
        }

        private void InitialRepaireAccessory(string accessory)
        {
            string[] accessorys = accessory.Split(';');
            for (int i = 0; i < accessorys.Count(); i++)
            {
                switch (i)
                {
                    case 0:
                        blljTextBox1.Text = accessorys[i];
                        break;
                    case 1:
                        blljTextBox2.Text = accessorys[i];
                        break;
                    case 2:
                        blljTextBox3.Text = accessorys[i];
                        break;
                    case 3:
                        blljTextBox4.Text = accessorys[i];
                        break;
                    case 4:
                        blljTextBox5.Text = accessorys[i];
                        break;
                }
            }
        }

        #region for problem analyze

        private string getProblemAnalyzeE()
        {
            string eStr = string.Empty;
            if (RadioButton11.Checked)
            {
                eStr = RadioButton11.Text;
            }
            return eStr;
        }

        private void SetProblemAnalyzeE(string value)
        {
            if (!string.IsNullOrEmpty(value))
            {
                if (value.Equals(RadioButton11.Text))
                {
                    RadioButton11.Checked = true;
                }
            }
        }

        private string getProblemAnalyzeD()
        {
            string dStr = string.Empty;
            if (RadioButton10.Checked)
            {
                dStr = RadioButton10.Text;
            }

            return dStr;
        }

        private void SetProblemAnalyzeD(string value)
        {
            if (!string.IsNullOrEmpty(value))
            {
                if (value.Equals(RadioButton10.Text))
                {
                    RadioButton10.Checked = true;
                }
            }
        }

        private string getProblemAnalyzeC()
        {
            string cStr = string.Empty;
            if (RadioButton9.Checked)
            {
                cStr = RadioButton9.Text;
            }
            else if (RadioButton8.Checked)
            {
                cStr = RadioButton8.Text;
            }
            else if (RadioButton13.Checked)
            {
                cStr = RadioButton13.Text;
            }

            return cStr;
        }

        private void SetProblemAnalyzeC(string value)
        {
            if (!string.IsNullOrEmpty(value))
            {
                if (value.Equals(RadioButton9.Text))
                {
                    RadioButton9.Checked = true;
                }
                if (value.Equals(RadioButton8.Text))
                {
                    RadioButton8.Checked = true;
                }
                if (value.Equals(RadioButton13.Text))
                {
                    RadioButton13.Checked = true;
                }
            }
        }

        private string getProblemAnalyzeB()
        {
            string bStr = string.Empty;
            if (RadioButton7.Checked)
            {
                bStr = RadioButton7.Text;
            }
            else if (RadioButton6.Checked)
            {
                bStr = RadioButton6.Text;
            }
            else if (RadioButton5.Checked)
            {
                bStr = RadioButton5.Text;
            }
            else if (RadioButton12.Checked)
            {
                bStr = RadioButton12.Text;
            }
            return bStr;
        }

        private void SetProblemAnalyzeB(string value)
        {
            if (!string.IsNullOrEmpty(value))
            {
                if (value.Equals(RadioButton5.Text))
                {
                    RadioButton5.Checked = true;
                }
                if (value.Equals(RadioButton6.Text))
                {
                    RadioButton6.Checked = true;
                }
                if (value.Equals(RadioButton7.Text))
                {
                    RadioButton7.Checked = true;
                }
                if (value.Equals(RadioButton12.Text))
                {
                    RadioButton12.Checked = true;
                }
            }
        }

        private string getProblemAnalyzeA()
        {
            string aStr = string.Empty;
            if (RadioButton4.Checked)
            {
                aStr = RadioButton4.Text;
            }
            else if (RadioButton3.Checked)
            {
                aStr = RadioButton3.Text;
            }
            else if (RadioButton2.Checked)
            {
                aStr = RadioButton2.Text;
            }
            else if (RadioButton1.Checked)
            {
                aStr = RadioButton1.Text;
            }
            return aStr;
        }

        private void SetProblemAnalyzeA(string value)
        {
            if (!string.IsNullOrEmpty(value))
            {
                if (value.Equals(RadioButton1.Text))
                {
                    RadioButton1.Checked = true;
                }
                if (value.Equals(RadioButton2.Text))
                {
                    RadioButton2.Checked = true;
                }
                if (value.Equals(RadioButton3.Text))
                {
                    RadioButton3.Checked = true;
                }
                if (value.Equals(RadioButton4.Text))
                {
                    RadioButton4.Checked = true;
                }
            }
        }

        #endregion

        protected void saveMaintainLinkButton_Click(object sender, EventArgs e)
        {
            bool isValid = SaveRepairInfo();
            if (isValid)
            {
                maintainRecordModalPopupExtender.Hide();
            }
            else
            {
                maintainRecordModalPopupExtender.Show();
            }
        }

        private void ClearWXForm()
        {
            blxxTextBox.Text = "";
            blyyRadioButtonList1.ClearSelection();
            blyyRadioButtonList2.ClearSelection();
            blyyRadioButtonList3.ClearSelection();
            blyyRadioButtonList4.ClearSelection();
            blyyRadioButtonList5.ClearSelection();
            blljTextBox1.Text = "";
            blljTextBox2.Text = "";
            blljTextBox3.Text = "";
            blljTextBox4.Text = "";
            blljTextBox5.Text = "";
            ljwzTextBox1.Text = "";
            ljwzTextBox2.Text = "";
            ljwzTextBox3.Text = "";
            ljwzTextBox4.Text = "";
            ljwzTextBox5.Text = "";
            ghRadioButton.Checked = false;
            thRadioButton.Checked = false;
            thSerialNumberTextBox.Text = "";
            thProductTypeTextBox.Text = "";
            RadioButton1.Checked = false;
            RadioButton2.Checked = false;
            RadioButton3.Checked = false;
            RadioButton4.Checked = false;
            RadioButton5.Checked = false;
            RadioButton6.Checked = false;
            RadioButton7.Checked = false;
            RadioButton8.Checked = false;
            RadioButton9.Checked = false;
            RadioButton10.Checked = false;
            RadioButton11.Checked = false;
            RadioButton12.Checked = false;
            RadioButton13.Checked = false;
            bjLabel.Text = "";
            bzTextBox.Text = "";
            Label1.Text = "";
        }

        private bool SaveRepairInfo()
        {
            bool isValid = true;
            try
            {
                ReturnToFactoryService returnService = new ReturnToFactoryService();
                ReturnToFactoryForm form = returnService.GetReturnToFactoryFormProductDetail(long.Parse(Request.QueryString[Constants.QUERYSTR_RETURNTOFACTORY_ID]));

                ApplicationProductService service = new ApplicationProductService();
                ApplicationProduct product = service.GetSimpleApplicatonProductByID(form.ApplicationProduct.ApplicationProductID);
                product.BLXX = blxxTextBox.Text;
                product.ReasonToProblem = getBLYY();
                product.RepairAccessory = getWXLJ();
                product.AccessoryPlace = getLJWZ();

                if (!string.IsNullOrEmpty(thSerialNumberTextBox.Text))
                {
                    product.ReplaceProductSerialNumber = thSerialNumberTextBox.Text;
                }
                if (!string.IsNullOrEmpty(thProductTypeTextBox.Text))
                {
                    product.ReplaceProductType = thProductTypeTextBox.Text;
                }
                if (versionDDL.SelectedIndex != 0)
                {
                    product.ReplaceProductVersion = versionDDL.SelectedValue;
                }
                if (thRadioButton.Checked)
                {
                    product.ChangeType = "替换";
                }
                if (ghRadioButton.Checked)
                {
                    product.ChangeType = "更换";
                }
                product.Remark = bzTextBox.Text;
                //product.RepairCompleteDateTime = DateTime.Now; // Chen Pu: 2010-06-02 不能在这里保存维修完成时间
                if (!product.ProblemAnalyzeReference.IsLoaded)
                {
                    product.ProblemAnalyzeReference.Load();
                }
                if (product.ProblemAnalyze == null)
                {
                    product.ProblemAnalyze = new ProblemAnalyze();
                }
                product.ProblemAnalyze.A = getProblemAnalyzeA();
                product.ProblemAnalyze.B = getProblemAnalyzeB();
                product.ProblemAnalyze.C = getProblemAnalyzeC();
                product.ProblemAnalyze.D = getProblemAnalyzeD();
                product.ProblemAnalyze.E = getProblemAnalyzeE();

                string valisdtaResult = string.Empty;
                valisdtaResult = ValidateMaintain();
                if (string.IsNullOrEmpty(ValidateMaintain()))
                {
                    service.UpdateProduct(product);
                }
                else
                {
                    Label1.Text = valisdtaResult;
                    isValid = false;
                }
            }
            catch (Exception ex)
            {
                LogUtility.LogUtilityIntance.LogException(ex, "保存维修记录");
                //MsgLbl.Text = string.Format("系统错误:{0}", ex.Message);
                isValid = false;
            }
            return isValid;
        }

        private string ValidateMaintain()
        {
            string result = string.Empty;
            if (string.IsNullOrEmpty(blxxTextBox.Text))
            {
                result += "请输入不良现象。";
            }
            if (ghRadioButton.Checked == false && thRadioButton.Checked == false)
            {
                if (RadioButton10.Checked == false)
                {
                    if (string.IsNullOrEmpty(getWXLJ()))
                    {
                        result += "请输入维修零件。";
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(blljTextBox1.Text))
                        {
                            if (blyyRadioButtonList1.SelectedItem == null)
                            {
                                result += "请选择第1个零件的不良原因";
                            }
                            else if (string.IsNullOrEmpty(ljwzTextBox1.Text) && blyyRadioButtonList1.SelectedIndex != 2)
                            {
                                result += "请输入第1个零件的维修位置";
                            }
                        }

                        if (!string.IsNullOrEmpty(blljTextBox2.Text))
                        {
                            if (blyyRadioButtonList2.SelectedItem == null)
                            {
                                result += "请选择第2个零件的不良原因";
                            }
                            else if (string.IsNullOrEmpty(ljwzTextBox2.Text) && blyyRadioButtonList2.SelectedIndex != 2)
                            {
                                result += "请输入第2个零件的维修位置";
                            }
                        }

                        if (!string.IsNullOrEmpty(blljTextBox3.Text))
                        {
                            if (blyyRadioButtonList3.SelectedItem == null)
                            {
                                result += "请选择第3个零件的不良原因";
                            }
                            else if (string.IsNullOrEmpty(ljwzTextBox3.Text) && blyyRadioButtonList3.SelectedIndex != 2)
                            {
                                result += "请输入第3个零件的维修位置";
                            }
                        }

                        if (!string.IsNullOrEmpty(blljTextBox4.Text))
                        {
                            if (blyyRadioButtonList4.SelectedItem == null)
                            {
                                result += "请选择第4个零件的不良原因";
                            }
                            else if (string.IsNullOrEmpty(ljwzTextBox4.Text) && blyyRadioButtonList4.SelectedIndex != 2)
                            {
                                result += "请输入第4个零件的维修位置";
                            }
                        }

                        if (!string.IsNullOrEmpty(blljTextBox5.Text))
                        {
                            if (blyyRadioButtonList5.SelectedItem == null)
                            {
                                result += "请选择第5个零件的不良原因";
                            }
                            else if (string.IsNullOrEmpty(ljwzTextBox5.Text) && blyyRadioButtonList5.SelectedIndex != 2)
                            {
                                result += "请输入第5个零件的维修位置";
                            }
                        }
                    }

                    if (string.IsNullOrEmpty(getProblemAnalyzeA()) && string.IsNullOrEmpty(getProblemAnalyzeB()) && string.IsNullOrEmpty(getProblemAnalyzeC()) && string.IsNullOrEmpty(getProblemAnalyzeD()) && string.IsNullOrEmpty(getProblemAnalyzeE()))
                    {
                        result += "请输入故障分析";
                    }
                }
            }

            return result;
        }

        private string getLJWZ()
        {
            string ljwz = string.Empty;
            if (!string.IsNullOrEmpty(ljwzTextBox1.Text))
            {
                ljwz += ljwzTextBox1.Text;
            }
            if (!string.IsNullOrEmpty(ljwzTextBox2.Text))
            {
                if (!string.IsNullOrEmpty(ljwz))
                {
                    ljwz += ";";
                }
                ljwz += ljwzTextBox2.Text;
            }
            if (!string.IsNullOrEmpty(ljwzTextBox3.Text))
            {
                if (!string.IsNullOrEmpty(ljwz))
                {
                    ljwz += ";";
                }
                ljwz += ljwzTextBox3.Text;
            }
            if (!string.IsNullOrEmpty(ljwzTextBox4.Text))
            {
                if (!string.IsNullOrEmpty(ljwz))
                {
                    ljwz += ";";
                }
                ljwz += ljwzTextBox4.Text;
            }
            if (!string.IsNullOrEmpty(ljwzTextBox5.Text))
            {
                if (!string.IsNullOrEmpty(ljwz))
                {
                    ljwz += ";";
                }
                ljwz += ljwzTextBox5.Text;
            }
            return ljwz;
        }

        private string getWXLJ()
        {
            string wxlj = string.Empty;
            if (!string.IsNullOrEmpty(blljTextBox1.Text))
            {
                wxlj += blljTextBox1.Text;
            }
            if (!string.IsNullOrEmpty(blljTextBox2.Text))
            {
                if (!string.IsNullOrEmpty(wxlj))
                {
                    wxlj += ";";
                }

                wxlj += blljTextBox2.Text;
            }
            if (!string.IsNullOrEmpty(blljTextBox3.Text))
            {
                if (!string.IsNullOrEmpty(wxlj))
                {
                    wxlj += ";";
                }

                wxlj += blljTextBox3.Text;
            }
            if (!string.IsNullOrEmpty(blljTextBox4.Text))
            {
                if (!string.IsNullOrEmpty(wxlj))
                {
                    wxlj += ";";
                }

                wxlj += blljTextBox4.Text;
            }
            if (!string.IsNullOrEmpty(blljTextBox5.Text))
            {
                if (!string.IsNullOrEmpty(wxlj))
                {
                    wxlj += ";";
                }

                wxlj += blljTextBox5.Text;
            }

            return wxlj;
        }

        private string getBLYY()
        {
            string blyy = string.Empty;
            if (blyyRadioButtonList1.SelectedItem != null)
            {
                if (!string.IsNullOrEmpty(blyy))
                {
                    blyy += ";";
                }
                blyy += blyyRadioButtonList1.SelectedValue;
            }
            if (blyyRadioButtonList2.SelectedItem != null)
            {
                if (!string.IsNullOrEmpty(blyy))
                {
                    blyy += ";";
                }
                blyy += blyyRadioButtonList2.SelectedValue;
            }
            if (blyyRadioButtonList3.SelectedItem != null)
            {
                if (!string.IsNullOrEmpty(blyy))
                {
                    blyy += ";";
                }
                blyy += blyyRadioButtonList3.SelectedValue;
            }
            if (blyyRadioButtonList4.SelectedItem != null)
            {
                if (!string.IsNullOrEmpty(blyy))
                {
                    blyy += ";";
                }
                blyy += blyyRadioButtonList4.SelectedValue;
            }
            if (blyyRadioButtonList5.SelectedItem != null)
            {
                if (!string.IsNullOrEmpty(blyy))
                {
                    blyy += ";";
                }
                blyy += blyyRadioButtonList5.SelectedValue;
            }

            return blyy;
        }

        protected void repairLinkButton_Click(object sender, EventArgs e)
        {
            maintainRecordModalPopupExtender.Show();
            ClearWXForm();
            BindData();
        }

        protected void CustomerQuatationGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
        }

        protected void clearReasonLinkButton_Click(object sender, EventArgs e)
        {
            blyyRadioButtonList1.SelectedIndex = -1;
            blyyRadioButtonList2.SelectedIndex = -1;
            blyyRadioButtonList3.SelectedIndex = -1;
            blyyRadioButtonList4.SelectedIndex = -1;
            blyyRadioButtonList5.SelectedIndex = -1;
        }

        protected void clearAnalyseLinkButton_Click(object sender, EventArgs e)
        {
            RadioButton1.Checked = false;
            RadioButton2.Checked = false;
            RadioButton3.Checked = false;
            RadioButton4.Checked = false;
            RadioButton5.Checked = false;
            RadioButton6.Checked = false;
            RadioButton7.Checked = false;
            RadioButton8.Checked = false;
            RadioButton9.Checked = false;
            RadioButton10.Checked = false;
            RadioButton11.Checked = false;
            RadioButton12.Checked = false;
            RadioButton13.Checked = false;
            RadioButton14.Checked = false;
            RadioButton15.Checked = false;
            RadioButton16.Checked = false;
        }

        protected void blxxTextBox_TextChanged(object sender, EventArgs e)
        {
            ApplicationProductService service = new ApplicationProductService();
            ApplicationProduct product = service.GetApplicationProductByID(long.Parse(hfAppliyProductID.Value));
            product.BLXX = blxxTextBox.Text;
            service.UpdateProduct(product);
        }

        protected void thProductTypeTextBox_TextChanged(object sender, EventArgs e)
        {
            if (thRadioButton.Checked && !string.IsNullOrEmpty(thProductTypeTextBox.Text))
            {
                blljTextBox1.Text = "替换：" + thProductTypeTextBox.Text;
            }
        }

        protected void ghRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            ApplicationProductService service = new ApplicationProductService();
            ApplicationProduct product = service.GetApplicationProductByID(long.Parse(hfAppliyProductID.Value));
            if (!product.ProductTypeReference.IsLoaded)
            {
                product.ProductTypeReference.Load();
            }
            blljTextBox1.Text = "替换：" + product.ProductType.ProductTypeName;
            thProductTypeTextBox.Text = "";
        }

        protected void blyyRadioButtonList1_SelectedChanged(object sender, EventArgs e)
        {
            if (blyyRadioButtonList1.SelectedIndex == 0)
            {
                ljwzTextBox1.Text = "0";
                blljTextBox1.Text = "Retest ok";
            }
            else if (blyyRadioButtonList1.SelectedIndex == 3)
            {
                ljwzTextBox1.Text = "0";
                blljTextBox1.Text = "软件升级";
            }
        }

        protected void blyyRadioButtonList2_SelectedChanged(object sender, EventArgs e)
        {
            if (blyyRadioButtonList2.SelectedIndex == 0)
            {
                ljwzTextBox2.Text = "0";
                blljTextBox2.Text = "Retest ok";
            }
            else if (blyyRadioButtonList1.SelectedIndex == 3)
            {
                ljwzTextBox2.Text = "0";
                blljTextBox2.Text = "软件升级";
            }
        }

        protected void blyyRadioButtonList3_SelectedChanged(object sender, EventArgs e)
        {
            if (blyyRadioButtonList3.SelectedIndex == 0)
            {
                ljwzTextBox3.Text = "0";
                blljTextBox3.Text = "Retest ok";
            }
            else if (blyyRadioButtonList1.SelectedIndex == 3)
            {
                ljwzTextBox3.Text = "0";
                blljTextBox3.Text = "软件升级";
            }
        }

        protected void blyyRadioButtonList4_SelectedChanged(object sender, EventArgs e)
        {
            if (blyyRadioButtonList4.SelectedIndex == 0)
            {
                ljwzTextBox4.Text = "0";
                blljTextBox4.Text = "Retest ok";
            }
            else if (blyyRadioButtonList4.SelectedIndex == 3)
            {
                ljwzTextBox4.Text = "0";
                blljTextBox4.Text = "软件升级";
            }
        }

        protected void blyyRadioButtonList5_SelectedChanged(object sender, EventArgs e)
        {
            if (blyyRadioButtonList5.SelectedIndex == 0)
            {
                ljwzTextBox5.Text = "0";
                blljTextBox5.Text = "Retest ok";
            }
            else if (blyyRadioButtonList4.SelectedIndex == 3)
            {
                ljwzTextBox5.Text = "0";
                blljTextBox5.Text = "软件升级";
            }
        }

        protected void bzTextBox_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(bzTextBox.Text))
            {
                ApplicationProductService service = new ApplicationProductService();
                ApplicationProduct product = service.GetApplicationProductByID(long.Parse(hfAppliyProductID.Value));
                product.Remark = bzTextBox.Text;

                service.UpdateProduct(product);
            }
        }
    }
}
